Back to course home
0% completed
Vote For New Content
Amazon Hard Problems
Constrained Subsequence Sum (hard)
Solution: Constrained Subsequence Sum
Number of Valid Subarrays (hard)
Solution: Number of Valid Subarrays
Count Vowels Permutation (hard)
Solution: Count Vowels Permutation
Concatenated Words (hard)
Solution: Concatenated Words
Minimum Replacements to Sort the Array (hard)
Solution: Minimum Replacements to Sort the Array
Count All Valid Pickup and Delivery Options (hard)
Solution: Count All Valid Pickup and Delivery Options
Length of the Longest Valid Substring (hard)
Solution: Length of the Longest Valid Substring
Count Array Pairs Divisible by K (hard)
Solution: Count Array Pairs Divisible by K
Word Ladder (hard)
Solution: Word Ladder
Path with Maximum Sum (hard)
Solution: Path with Maximum Sum
Maximize Distance to Closest Person (medium)
Problem Statement
You are given a binary array seats representing the row of seats, where seats[i] = 0 represents that the ith seat is empty, and seats[i] = 1 represents a person sitting in the ith seat. It is given that seats array contains at least one 1 and 0.
You want to sit in any seat such that the distance between you and the closest person to you is maximized.
Return that maximum distance to the closest person.
Examples
-
Example 1:
- Input: seats =
[1,0,0,0,1,0,0] - Expected Output:
2 - Justification: The best seat is either of the two at the index 2 or 6, as they are both 2 seats away from the nearest occupied seat.
- Input: seats =
-
Example 2:
- Input: seats =
[1,0,0,0,0,1] - Expected Output:
2 - Justification: The middle seats (index 2 or 3) are equally optimal, being 2 seats away from any occupied seat.
- Input: seats =
-
Example 3:
- Input: seats =
[0,1,0,1,0,1,0,0,0,0] - Expected Output:
4 - Justification: The best position is at the end (index 9), which is 4 seats away from the nearest person.
- Input: seats =
Try it yourself
Try solving this question here:
Java
. . . .
Solution: Reverse Integer
Solution: Maximize Distance to Closest Person
Mark as Completed
On this page
Problem Statement
Examples
Try it yourself